package com.demo.stream_;

import org.junit.Test;

import javax.xml.bind.SchemaOutputResolver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gy
 * @date 2022/5/10
 * 筛选，是按照一定的规则校验流中的元素，
 * 将符合条件的元素提取到 新的流中的 操作
 */
public class StreamFilter {
    @Test
    public void m1(){
        List<Integer> list = Arrays.asList(6, 7, 3, 8, 1, 2, 9);
        // 筛选集合中 大于7的元素
        list.stream().filter(x -> x > 7).forEach(System.out::println);
    }

    /**
     * 筛选员工中工资高于8000的人，并形成新的集合。 形成新集合依赖collect（收集）
     */
    @Test
    public void m2(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

        List<String> filterList = personList.stream().filter(person -> person.getSalary() > 8000)
                .map(Person::getName)
                .collect(Collectors.toList());

        System.out.print("高于8000的员工姓名：" + filterList);
    }


    @Test
    public void m3(){
        System.out.println("--- code 1 ---");
        List<String> list = Arrays.asList("Varanasi", "Madurai", "Agartala");
        list.parallelStream()
                .limit(2)
                .forEach(s->System.out.printf("%s ", s));

        System.out.println("\n--- code 2 ---");
        List<Integer> numList = Arrays.asList(41, 42, 43, 44);
        numList.parallelStream()
                .mapToInt(i -> i * 10)
                .limit(3)
                .forEach(s->System.out.printf("%s ", s));

        System.out.println("\n--- code 3 ---");
        numList.stream()
                .mapToInt(i -> i * 10)
                .limit(3)
                .forEach(s->System.out.printf("%s ", s));
    }
}
